package DatabaseImp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Database.DBConnecter;
import DatabasePO.AccountPO;
import DatabasePO.StudentPO;



public class StudentDBImpl {
	
	DBConnecter dbConnecter = DBConnecter.getInstance();
	
	Connection connection = null;
	PreparedStatement preparedStatement = null;
	ResultSet resultSet = null;
	
	
	public StudentPO queryStudent(AccountPO accountPO){
		StudentPO studentPO = null;
		
		connection = dbConnecter.getConnecter();
		String sql = "select * from student where Sacc = ?";
		
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, accountPO.getAccount());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next()){
				studentPO = new StudentPO();
				studentPO.setNumber(resultSet.getString("Sno"));
				studentPO.setName(resultSet.getString("Snm"));
				studentPO.setSex(resultSet.getString("Sex"));
				studentPO.setDepartment(resultSet.getString("Sde"));
				studentPO.setAccount(resultSet.getString("Sacc"));
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally{
			dbConnecter.closeConnecter(connection);
			dbConnecter.closeResultSet(resultSet);
			dbConnecter.closePrepareStatement(preparedStatement);
		}
		
		return studentPO;
	}

}
